package com.cqs.learning.classload;


import java.math.BigDecimal;

/**
 * -XX:+PrintGCDetails -XX:+TraceClassUnloading -Xmx10M -XX:-PrintFlagsFinal
 * 类卸载的例子
 * @author lixiaowen
 * @date 2019/9/20
 */
public class UnloadClass {

    public final static int id = 1;

    public static void main(String[] args)  {
        unloadClass();
    }
    private static void unloadClass(){
        //自定义类加载器
        ClassLoader classLoader = new MyClassLoader();
        Class<?> aClass = null;
        try {
            aClass = classLoader.loadClass("com.cqs.learning.classload.UnloadClass");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(aClass.getClassLoader());
        for (int i = 0; i < 200000; i++) {
            UnloadClass unloadClass = new UnloadClass();
            BigDecimal bigDecimal = new BigDecimal(i);
            String str = String.valueOf(i);
        }
        //类的class对象可回收的状态
        aClass = null;
        //加载器可以回收
        classLoader = null;
        System.gc();
    }
}
